<?php
/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\index\logic;

use app\common\logic\BaseLogic;
use app\common\model\base\BaseModel;
use app\common\model\Plan;
use app\common\traits\InstanceTrait;
use Helper\DateHelper;
use think\facade\Db;

/**
 * Class PlanLogic
 * @package app\index\logic
 * @property Plan $model
 */
class PlanLogic extends BaseLogic
{
    use InstanceTrait;

    public function __construct($model = NULL)
    {
        $this->model = Plan::instance();
        parent::__construct($model);
    }

    /**
     * 获取列表
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getList($param)
    {
        sdump(empty((array)new \stdClass()));
        $Plan = Plan::instance();
        $data = $Plan->getList($param);
        return $data;
    }

    /**
     * 获取统计
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getCount($param)
    {
        $data = $this->model->getCount($param);
        return $data;
    }

    /**
     * 获取ID
     * @param $param
     * @return BaseModel|null
     */
    public function getByID($param)
    {
        $data = $this->model->getByID($param[$this->model->getPk()] ?? $param['id']);
        return $data;
    }

    /**
     * 插入数据
     * @param $param
     * @return bool
     */
    public function createData($param)
    {
        $data = Plan::create($param);
        return $data;
    }

    /**
     * 更新数据
     * @param $param
     * @return bool
     */
    public function updateByID($param)
    {
        $plan_one = Plan::find($param[$this->model->getPk()]);
        if (!empty($plan_one)) {
            if ($plan_one['status'] != $param['status'] && $plan_one['status'] == 2) {
                $this->writeCycle($plan_one);
            }
            $plan_one->save($param);
        }
        return TRUE;
    }

    /**
     * 删除数据
     * @param $id
     * @return bool
     */
    public function delByID($id)
    {
        $logic = $this;
        Db::transaction(function () use ($id, $logic) {
            $one = Plan::find($id);
            if (!empty($one)) {
                //清理子任务
                $children_list = Plan::getTheList(['pid' => $id]);
                foreach ($children_list as $key => $value) {
                    $this->delByID($value['plan_id']);
                }
                Plan::destroy($id);
            }
        });
        return true;
    }

    /**
     * 批量删除
     * @param $param
     * @return bool
     */
    public function delByIDs($param)
    {
        $data = $this->model->delByIDs($param[$this->model->getPk() . 's'] ?? $param['ids'], FALSE);
        return $data;
    }

    /**
     * 启用禁用
     * @param $param
     * @return bool
     */
    public function enableByIDs($param)
    {
        $data = $this->model->enableByIDs(
            $param[$this->model->getPk() . 's'] ?? $param['ids'],
            $param['status'],
            FALSE);
        return $data;
    }

    /**
     * 更改挑战的状态,如果有周期则写入新的挑战
     * @param type $user_id
     * @param type $param
     * @return type
     */
    public function finish($param)
    {
        $Plan = new Plan();
        $plan_model = $Plan->find($param['id']);
        if (!empty($plan_model)) {
            $logic = $this;
            Db::transaction(function () use ($plan_model, $logic) {
                $data = [
                    'status' => 2,
                    'finish_time' => $_SERVER['REQUEST_TIME'],
                ];
                $plan_model->save($data);
                $logic->writeCycle($plan_model);
            });
            return TRUE;
        }
    }

    /**
     * 写入新的挑战
     * @param type $plan_model
     */
    protected function writeCycle($plan_model)
    {
//        判断挑战状态和周期
        if ($plan_model['status'] == 2 && !empty($plan_model['cycle'])) {
            $plan_time = DateHelper::toTime($plan_model['plan_time']);
            $endline = DateHelper::toTime(DateHelper::to_date($_SERVER['REQUEST_TIME'], "Y-m-d 23:59:59"));
//              生成任务日期必须是今天往后
            while ($plan_time <= $endline) {
                $plan_time = strtotime("+ " . $plan_model['cycle'] . " day", $plan_time);
            }
            $data = [
                'status' => 1,
                'plan_time' => $plan_time,
                'content' => $plan_model['content'],
                'cycle' => $plan_model['cycle'],
                'member_id' => $plan_model['member_id'],
                'pid' => $plan_model['plan_id'],
            ];
            $one = Plan::where($data)->find();
            if (empty($one)) {
                //不存在相同的才插入
                Plan::create($data);
            }
        }
    }
}
